package com.xubeizhi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xubeizhi.common.PageBean;
import com.xubeizhi.common.Result;
import com.xubeizhi.entity.Debt;
import com.xubeizhi.mapper.DebtMapper;
import com.xubeizhi.service.DebtService;
import com.xubeizhi.utils.DataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DebtServiceImpl extends ServiceImpl<DebtMapper, Debt> implements DebtService {

    @Autowired
    private DebtMapper debtMapper;

    /**
     * 新增欠款账单
     *
     * @param debt
     * @return
     */
    @Override
    public Result CreateDept(Debt debt) {
        BeanUtils.copyProperties(debt, Debt.class);
        debt.setCreateTime(DataUtils.getDateNow());
        debt.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.insert(debt);
        if (rows > 0) {
            Result.success(0, "操作成功", debt);
        } else {
            Result.error(1, "操作失败", null);
        }
        return Result.success(rows);
    }

    /**
     * 删除账单方法
     *
     * @param debt
     * @return
     */
    @Override
    public Result deleteDept(Debt debt) {
//        BeanUtils.copyProperties(debt,Debt.class);
        debt.setUpdateTime(DataUtils.getDateNow());
        debt.setRemoveStatus("1");
        int rows = baseMapper.updateById(debt);
        return rows > 0 ? Result.success(0, "操作成功", debt) : Result.error(1, "操作失败", null);
    }

    /**
     * 修改欠款账单
     *
     * @param debt
     * @return
     */
    @Override
    public Result updateByIdDept(Debt debt) {
        debt.setUpdateTime(DataUtils.getDateNow());
        int rows = baseMapper.updateById(debt);
        return rows > 0 ? Result.success(0, "操作成功", debt) : Result.error(1, "操作失败", null);
    }

    /**
     * 根据姓名和状态查询欠款账单
     *
     * @param pageNum
     * @param pageSize
     * @param name     姓名
     * @param status   状态
     * @return
     */
    @Override
    public Result<PageBean<Debt>> listDebt(Integer pageNum, Integer pageSize, String name, String status, String removeStatus) {
        //创建pageBean对象
        PageBean<Debt> db = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        //调用mapper
        List<Debt> debts = debtMapper.listDebt(name, status, removeStatus);
        Page<Debt> p = (Page<Debt>) debts;

        db.setTotal(p.getTotal());
        db.setItems(p.getResult());


        return Result.success(0, "查询成功", db);
    }

    /**
     * 分页获取列表
     *
     * @param pageNum
     * @param pageSize
     * @param id
     * @param status
     * @return
     */
    @Override
    public Result<PageBean<Debt>> list(Integer pageNum, Integer pageSize, Integer id, String status) {
        PageBean<Debt> page = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<Debt> debts = debtMapper.list();
        Page<Debt> p = (Page<Debt>) debts;
        page.setTotal(p.getTotal());
        page.setItems(p.getResult());
        return Result.success(0, "获取成功", page);
    }

    /**
     * 根据姓名和时间搜索欠款账单
     *
     * @param name
     * @param
     * @return
     */
    @Override
    public Result<PageBean<Debt>> searchDebt(String name, String startDate, String endDate) {

        List<Debt> debts = debtMapper.searchList(name, startDate, endDate);
        if (debts != null) {
            return Result.success(0, "查询成功", debts);
        }
        return Result.error(1, "查询失败", null);
    }
}
